package com.csi.controller.user;

import com.alibaba.fastjson2.JSON;
import com.csi.pojo.Result;
import com.csi.service.UserService;
import com.csi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 根据手机号解冻账号
 */
@WebServlet("/user/unfreezeAccountByPhoneController")
public class UnfreezeAccountByPhoneController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");

        PrintWriter out = resp.getWriter();

        UserService userService = new UserServiceImpl();

        String phone = req.getParameter("phone");

        if (phone == null || phone.isEmpty()) {
            Result result = new Result<>();
            result.setCode(400);
            result.setMsg("手机号不能为空");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        Integer userId = userService.queryUserIdByPhone(phone);

        userService.updateStatus(1,userId);

        Result result = new Result<>();
        result.setCode(200);
        result.setMsg("解冻成功");

        out.print(JSON.toJSONString(result));

        out.flush();
        out.close();
    }
}
